﻿#region using...
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Infusion.Silverlight.Controls.OpenOfficeXML;
#endregion

namespace Infusion.Silverlight.Controls.Input
{
    #region : DocumentPage :
    #region TemplateParts...
    [TemplatePart(Name = DocumentPage.CanvasPage, Type = typeof(Canvas))]
    [TemplatePart(Name = DocumentPage.RectPageBorder, Type = typeof(Rectangle))]
    [TemplatePart(Name = DocumentPage.TbxPageText, Type = typeof(TextBox))]
    #endregion
    public class DocumentPage : Control
    {
        #region TemplatePart Names...
        public const string CanvasPage = "CanvasPage";
        public const string RectPageBorder = "RectPageBorder";
        public const string TbxPageText = "TbxPageText";
        #endregion

        #region TemplatePart Variables...
        internal Canvas canvasPage;
        internal Rectangle rectPageBorder;
        public TextBox tbxPageText;
        #endregion

        #region Dependency Properties...
        #region Size
        /// <summary>
        /// Identifies the Infusion.Silverlight.Controls.Input.DocumentPage.Size dependency property.
        /// </summary>
        public static readonly DependencyProperty SizeProperty = DependencyProperty.Register(
            "Size",
            typeof(PageSize),
            typeof(DocumentPage),
            null);
        #endregion

        #region LeftMargin
        /// <summary>
        /// Identifies the Infusion.Silverlight.Controls.Input.DocumentPage.LeftMargin dependency property.
        /// </summary>
        public static readonly DependencyProperty LeftMarginProperty = DependencyProperty.Register(
            "LeftMargin",
            typeof(int),
            typeof(DocumentPage),
            null);
        #endregion

        #region RightMargin
        /// <summary>
        /// Identifies the Infusion.Silverlight.Controls.Input.DocumentPage.RightMargin dependency property.
        /// </summary>
        public static readonly DependencyProperty RightMarginProperty = DependencyProperty.Register(
            "RightMargin",
            typeof(int),
            typeof(DocumentPage),
            null);
        #endregion

        #region TopMargin
        /// <summary>
        /// Identifies the Infusion.Silverlight.Controls.Input.DocumentPage.TopMargin dependency property.
        /// </summary>
        public static readonly DependencyProperty TopMarginProperty = DependencyProperty.Register(
            "TopMargin",
            typeof(int),
            typeof(DocumentPage),
            null);
        #endregion

        #region BottomMargin
        /// <summary>
        /// Identifies the Infusion.Silverlight.Controls.Input.DocumentPage.BottomMargin dependency property.
        /// </summary>
        public static readonly DependencyProperty BottomMarginProperty = DependencyProperty.Register(
            "BottomMargin",
            typeof(int),
            typeof(DocumentPage),
            null);
        #endregion
        #endregion

        #region Class Variables...
        private bool templateApplied = false;
        private Document document = null;
        #endregion

        #region Constructor(s)...
        #region DocumentPage
        public DocumentPage() : base()
        {
            DefaultStyleKey = typeof(DocumentPage);
            Size = new PageSizeLetter();

            LeftMargin =
                RightMargin =
                TopMargin =
                BottomMargin = 72; //default margin = 72 points = 1 inch;
        }
        #endregion
        #endregion

        #region Event Handlers...
        #region tbxPageText_GotFocus
        private void tbxPageText_GotFocus(object sender, RoutedEventArgs e)
        {
            if (document != null)
                document.ActivePageIndex = document.Pages.IndexOf(this);
        }
        #endregion
        #endregion

        #region Methods...
        #region OnApplyTemplate
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (!templateApplied)
            {
                canvasPage = (Canvas)GetTemplateChild(CanvasPage);
                rectPageBorder = (Rectangle)GetTemplateChild(RectPageBorder);
                tbxPageText = (TextBox)GetTemplateChild(TbxPageText);

                Width = Size.PixelWidth;
                Height = Size.PixelHeight;

                tbxPageText.SetValue(Canvas.LeftProperty, (double)LeftMargin + 19d);
                tbxPageText.SetValue(Canvas.TopProperty, (double)TopMargin + 19d);
                tbxPageText.Width = Width - (double)LeftMargin - (double)RightMargin - (2d * 19d);
                tbxPageText.Height = Height - (double)TopMargin - (double)BottomMargin - (2d * 19d);
                tbxPageText.GotFocus += new RoutedEventHandler(tbxPageText_GotFocus);

                templateApplied = true;
            }
        }
        #endregion
        #endregion

        #region Properties...
        #region Size
        public PageSize Size
        {
            get { return (PageSize)GetValue(SizeProperty); }
            set { SetValue(SizeProperty, value); }
        }
        #endregion

        #region LeftMargin
        public int LeftMargin
        {
            get { return (int)GetValue(LeftMarginProperty); }
            set { SetValue(LeftMarginProperty, value); }
        }
        #endregion

        #region RightMargin
        public int RightMargin
        {
            get { return (int)GetValue(RightMarginProperty); }
            set { SetValue(RightMarginProperty, value); }
        }
        #endregion

        #region TopMargin
        public int TopMargin
        {
            get { return (int)GetValue(TopMarginProperty); }
            set { SetValue(TopMarginProperty, value); }
        }
        #endregion

        #region BottomMargin
        public int BottomMargin
        {
            get { return (int)GetValue(BottomMarginProperty); }
            set { SetValue(BottomMarginProperty, value); }
        }
        #endregion

        #region Orientation
        public PageOrientation Orientation
        {
            get { return Size.Orientation; }
            set { Size.Orientation = value; }
        }
        #endregion

        #region Document
        public Document Document
        {
            internal set { document = value; }
            get { return document; }
        }
        #endregion
        #endregion
    }
    #endregion
}